package utils;


import org.springframework.util.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;


/**
 * 日期工具类
 * @author gaoyb
 */
public class DateUtils {

    /**
     * 日期 格式化:yyyy-MM-dd
     */
    public static final String DATE = "yyyy-MM-dd";
    /**
     * 日期 格式化:yyyyMMdd
     */
    public static final String DATE_WITHOUT_SEPERATOR = "yyyyMMdd";

    /**
     * 日期-小时 格式化
     */
    public static final String DATE_HOUR = "yyyyMMddHH";

    public static final String DATE_HOUR_SECOND = "yyyyMMddHHmmss";

    public static final String DATE_HOUR_NANOSECOND = "yyyyMMddHHmmssSSS";

    /**
     * 日期小时分钟 格式化
     */
    public static final String DATE_SPACE_MINUTE = "yyyy-MM-dd HH:mm";
    /**
     * 日期时间 格式化
     */
    public static final String TIMESTAMP = "yyyy-MM-dd HH:mm:ss";

    /**
     * 日期时间 格式化
     */
    public static final String DATETIME = "yyyyMMdd HH:mm:ss";

    /**
     * 1周的天数
     */
    public static final int ONE_WEEK_DAYS = 7;
    /**
     * 一秒的毫秒数
     */
    public static int ONE_SECOND_MILLI_SECONDS = 1000;
    /**
     * 一分钟的秒数
     */
    public static int ONE_MINUTE_SECONDS = 60;
    /**
     * 一分钟的毫秒数
     */
    public static int ONE_MINUTE_MILLISECONDS = ONE_MINUTE_SECONDS * ONE_SECOND_MILLI_SECONDS;
    /**
     * 一小时的毫秒数
     */
    public static final int ONE_HOUR_MILLISECONDS = 60 * 60 * 1000;
    /**
     * 一天的秒数
     */
    public static int ONE_DAY_SECONDS = 24 * 60 * 60;
    /**
     * 一周的秒数
     */
    public static int ONE_WEEK_SECONDS = ONE_DAY_SECONDS * ONE_WEEK_DAYS;

    private static final int LAST_SECOND_OF_MINUTE = 59;
    private static final int LAST_MINUTE_OF_HOUR = 59;
    private static final int LAST_HOUR_OF_DAY = 23;
    
    
    private static final long MILLI_SECS_PER_SEC = 1000;
    private static final long MILLI_SECS_PER_MIN = 60 * MILLI_SECS_PER_SEC;
    private static final long MILLI_SECS_PER_HOUR = 60 * MILLI_SECS_PER_MIN;
    private static final long MILLI_SECS_PER_DAY = 24 * MILLI_SECS_PER_HOUR;


    /**
     * 转换时间
     *
     * @param format
     * @param date
     * @return
     */
    public static String parseDate2Str(String format, Date date) {
    	if(date == null){
    		return "";
    	}
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    public static Date parseStr2Date(String format, String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        try {
			return simpleDateFormat.parse(date);
		} catch (ParseException e) {
			//  Auto-generated catch block
			e.printStackTrace();
		}
        return null;
    }

    /**
     * 将 java.time.LocalDateTime 转为指定格式的时间字串
     */
    public static String localDateTime2Str(LocalDateTime localDateTime, String pattern) {
        if (localDateTime == null || pattern == null) {
            return "";
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return formatter.format(localDateTime);
    }

    /**
     * 将 java.time.LocalDate 转为指定格式的时间字串
     */
    public static String localDate2Str(LocalDate localDate, String pattern) {
        if (localDate == null || pattern == null) {
            return "";
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return formatter.format(localDate);
    }

    /**
     * 当前创建时间（yyyy-MM-dd HH:mm:ss）
     *
     * @return
     */
    public static String currentDate() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TIMESTAMP);
        return simpleDateFormat.format(new Date());
    }

    public static String currentDate(String formate) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formate);
        return simpleDateFormat.format(new Date());
    }

    /**
     * 以当期日期为基准，返回指定日期类型日期增量日期。
     *
     * @param dateType 日期类型
     * @param count    增加数量，可以为负数
     * @return
     */
    public static Date getMagicDate(int dateType, int count) {
        Calendar calendar = new GregorianCalendar();
        Date todayTime = new Date();
        calendar.setTime(todayTime);
        calendar.add(dateType, count);
        return calendar.getTime();
    }

    /**
     * 返回指定日期类型日期增量日期
     *
     * @param dateType 日期类型
     * @param count    增加数量，可以为负数
     * @param baseDate 参照日期
     * @return
     */
    public static Date getMagicDate(int dateType, int count, Date baseDate) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(baseDate);
        calendar.add(dateType, count);
        return calendar.getTime();
    }

    /**
     * 日期字符串转换为日期格式，转换出错时将返回null。
     *
     * @param dateStr    日期字符串
     * @param dateFormat 日期格式
     * @return date对象
     */
    public static Date strToDate(String dateStr, String dateFormat) {
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        Date date = null;
        try {
            date = format.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     *
     * @param month -1:前一月,0:当前月,1:后一个月
     * @param dateFormat
     * @return
     */
    public static String getFirstDayOfmonth(int month,String dateFormat){
        Calendar cal_1= Calendar.getInstance();
        cal_1.add(Calendar.MONTH, month);
        cal_1.set(Calendar.DAY_OF_MONTH,1);
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        return format.format(cal_1.getTime());
    }

    /**
     * 将时间戳转为"yyyy-MM-dd HH:mm:ss"格式的时间字串
     *
     * @param mill 时间戳
     * @return "yyyy-MM-dd HH:mm:ss"格式的时间字串
     */
    public static String stampToSecondStr(long mill) {
        Date date = new Date(mill);
        String strs = "";
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(TIMESTAMP);
            strs = sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strs;
    }

    /**
     * 日期转换为指定格式的字符
     *
     * @param date       日期
     * @param dateFormat 日期格式
     * @return
     */
    public static String dateToString(Date date, String dateFormat) {
        String returnValue = "";
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(dateFormat);
            returnValue = df.format(date);
        }
        return returnValue;
    }

    /**
     * 日期字符串转换为日期格式
     *
     * @param dateStr    日期字符串
     * @param dateFormat 日期格式
     * @return
     * @throws ParseException
     */
    public static Date stringToDate(String dateStr, String dateFormat) {
        SimpleDateFormat sf = new SimpleDateFormat(dateFormat);
        try {
            return sf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 传入格式，返回今天的日期字串
     */
    public static String getTodayStr(String format) {
        String todayStr = dateToString(new Date(), format);
        return todayStr;
    }

    /**
     * @return 时间
     * @brief 获取一个时间，这个时间是今天的第0秒
     */
    public static Date getTodayAtFirstSecond() {
        Calendar now = Calendar.getInstance();
        now.setTimeInMillis(System.currentTimeMillis());
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }

    /**
     * @return 时间
     * @brief 获取一个时间，这个时间是今天的最后一秒
     */
    public static Date getTodayAtLastSecond() {
        Calendar now = Calendar.getInstance();
        now.setTimeInMillis(System.currentTimeMillis());
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH), LAST_HOUR_OF_DAY,
                LAST_MINUTE_OF_HOUR, LAST_SECOND_OF_MINUTE);
        return cal.getTime();
    }

    /**
     * 获取昨天的日期，时分秒与此刻相同。
     *
     * @return 昨天的日期，时分秒与此刻相同。
     */
    public static Date getYestodayDate() {
        Date yestoday = getMagicDate(Calendar.DAY_OF_MONTH, -1);
        return yestoday;
    }

    /**
     * 获取昨天第一秒的日期，即yyyy-MM-dd 00:00:00.
     *
     * @return 昨天第一秒的日期，即 yyyy-MM-dd 00:00:00.
     */
    public static Date getYestodayFirstTime() {
        // 获取昨天的日期，时分秒与此刻相同。
        Date yestoday = getYestodayDate();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(yestoday);
        // 时间设置为 00:00:00
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date yestodayFirstTime = calendar.getTime();

        return yestodayFirstTime;
    }

    /**
     * 获取昨天第一秒的日期，即 yyyy-MM-dd 00:00:00.
     *
     * @return 昨天第一秒的日期，即 yyyy-MM-dd 00:00:00.
     */
    public static String getYestodayFirstTimeStr() {
        Date yestodayFirstTime = getYestodayFirstTime();
        return dateToString(yestodayFirstTime, TIMESTAMP);
    }

    /**
     * 获取昨天最后一秒的日期，即 yyyy-MM-dd 23:59:59.
     *
     * @return 昨天最后一秒的日期，即 yyyy-MM-dd 23:59:59.
     */
    public static Date getYestodayLastTime() {
        // 获取昨天的日期，时分秒与此刻相同。
        Date yestoday = getYestodayDate();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(yestoday);
        // 时间设置为23:59:59
        calendar.set(Calendar.HOUR_OF_DAY, LAST_HOUR_OF_DAY);
        calendar.set(Calendar.MINUTE, LAST_MINUTE_OF_HOUR);
        calendar.set(Calendar.SECOND, LAST_SECOND_OF_MINUTE);
        Date yestodayLastTime = calendar.getTime();

        return yestodayLastTime;
    }

    /**
     * 获取昨天最后一秒的日期，即 yyyy-MM-dd 23:59:59.
     *
     * @return 昨天最后一秒的日期，即 yyyy-MM-dd 23:59:59.
     */
    public static String getYestodayLastTimeStr() {
        Date yestodayEnd = getYestodayLastTime();
        return dateToString(yestodayEnd, TIMESTAMP);
    }

    /**
     * 将yyyy-MM-dd HH:mm:ss时间格式的日期转化为LocalDateTime
     *
     * @param dateTime yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static LocalDateTime parseStr2LocalDate(String dateTime) {
        if (StringUtils.isEmpty(dateTime)) {
            return null;
        }
        return LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern(TIMESTAMP));
    }

    /**
     * <p>获取当前时间</p>
     *
     * @param pattern - 时间格式
     * @return - 时间字符串
     */
    public static String queryCurrentTime(String pattern) {
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(Calendar.getInstance().getTime());
    }

    public static int compare_date(Date date1, Date date2) {
            if (date1.getTime() > date2.getTime()) {
                return 1;
            } else if (date1.getTime() < date2.getTime()) {
                return -1;
            } else {
                return 0;
            }

    }

    //时间比较
    public static int compare_date(String date1, String date2) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date d1 = df.parse(date1);
            Date d2 = df.parse(date2);
            if (d1.getTime() > d2.getTime()) {

                return 1;
            } else if (d1.getTime() < d2.getTime()) {

                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;

    }

    public static int compare_Time(String date1, String date2, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        try {
            Date d1 = df.parse(date1);
            Date d2 = df.parse(date2);
            if (d1.getTime() > d2.getTime()) {

                return 1;
            } else if (d1.getTime() < d2.getTime()) {

                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;

    }

    /**
     * 将时间字串转为 LocalDateTime，时间字串的格式请用 pattern 指定
     */
    public static LocalDateTime str2LocalDateTime(String datetimeStr, String pattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        LocalDateTime localDateTime = LocalDateTime.parse(datetimeStr, dateTimeFormatter);
        return localDateTime;
    }

    public static DateTimeFormatter getTimeStampDateTimeFormatter(){
        return DateTimeFormatter.ofPattern(TIMESTAMP);
    }

    public static long getMsByDateStr(String   dateStr){
    	Date d=null;
        try{
            SimpleDateFormat sf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            d=sf.parse(dateStr);
        }
        catch(Exception   e){
        	e.printStackTrace();
        }
        return d.getTime();
    }

    /**
     * 
     * @Title: getMonthSpace 
     * @Description: 获取两个日照之间的月数
     * @param date1
     * @param date2
     * @return
     * @throws ParseException
     * @return: int
     */
    public static int getMonthSpace(String date1, String date2) {

        int result = 0;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();

        try {
			c1.setTime(sdf.parse(date1));
			c2.setTime(sdf.parse(date2));
		} catch (ParseException e) {
			e.printStackTrace();
		}
        result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);

        return result == 0 ? 1 : Math.abs(result);

    }
    
    public static long getDiffHours(Date begin, Date end) {
		return (end.getTime() - begin.getTime()) % MILLI_SECS_PER_DAY / MILLI_SECS_PER_HOUR; 
	}
	
	public static long getDiffMins(Date begin, Date end) {
		return (end.getTime() - begin.getTime()) % MILLI_SECS_PER_DAY % MILLI_SECS_PER_HOUR / MILLI_SECS_PER_MIN;
	}
	
	public static long getDiffDays(Date begin, Date end) {
		return (end.getTime() - begin.getTime()) / MILLI_SECS_PER_DAY;
	}
	
	public static long getDiffSecs(Date begin, Date end) {
		return (end.getTime() - begin.getTime()) % MILLI_SECS_PER_DAY % MILLI_SECS_PER_HOUR % MILLI_SECS_PER_MIN / MILLI_SECS_PER_SEC;
	}
	
	public static long getMinsBetweenTwoDates(Date begin, Date end) {
		return (end.getTime() - begin.getTime()) / (MILLI_SECS_PER_MIN);
	}
	/**
	 * 
	 * @Title: getDateDiff 
	 * @Description: 
	 * @param endDate
	 * @param nowDate
	 * @return
	 * @return: String
	 */
	public static String getDateDiff(Date endDate, Date nowDate) {
		if(endDate==null||nowDate==null){
			return null;
		}
	    long nd = 1000 * 24 * 60 * 60;
	    long nh = 1000 * 60 * 60;
	    long nm = 1000 * 60;
	    // long ns = 1000;
	    // 获得两个时间的毫秒时间差异
	    long diff = endDate.getTime() - nowDate.getTime();
	    // 计算差多少天
	    long day = diff / nd;
	    // 计算差多少小时
	    long hour = diff % nd / nh;
	    // 计算差多少分钟
	    long min = diff % nd % nh / nm + (diff % nd % nh % nm>0?1:0);
	    
	    // 计算差多少秒//输出结果
	    // long sec = diff % nd % nh % nm / ns;
	    StringBuffer result = new StringBuffer();
	    if(day>0){
	    	result.append(day + "天");
	    }
	    if(hour>0){
	    	result.append( hour + "小时");
	    }
	    if(min>0){
	    	result.append(min + "分钟");
	    }
	    return result.toString();
	}
	public static String getDateStrBySecond(long duration){
	    //天数计算
		long days = (duration)/(24*3600);
	    //小时计算
		long hours = (duration)%(24*3600)/3600;
	    //分钟计算
		long minutes = (duration)%3600/60;
	    StringBuffer result = new StringBuffer();
	    if(days>0){
	    	result.append(days + "天");
	    }
	    if(hours>0){
	    	result.append( hours + "小时");
	    }
	    if(minutes>0){
	    	result.append(minutes + "分钟");
	    }
	    return result.toString();
	    
	}
	public static boolean judgeBirthday(Date birthday){ 
        if(birthday==null){
        	return false;
        }
		Date today = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
        if(sdf.format(today).equals(sdf.format(birthday))){
        	return true;
        }
        return false;
	}
	
	// 获得某天最大时间 2017-10-15 23:59:59
	public static Date getEndOfDay(Date date) {
		LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());;
		LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
		return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
	}
	
	// 获得某天最小时间 2017-10-15 00:00:00
	public static Date getStartOfDay(Date date) {
		LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
		LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
		return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
	}
	
	/**
     * 获取本月第一天的0点0分0秒
    * @Title：getTheFirstDayOfMonth 
    * @Description：TODO
    * @param ：@return 
    * @return ：Date 
    * @throws
     */
    public static Date getTheFirstDayOfMonth(){
    	Calendar cal = Calendar.getInstance();
    	cal.add(Calendar.MONTH, 0);
    	cal.set(Calendar.DAY_OF_MONTH, 1);
    	cal.set(Calendar.HOUR_OF_DAY, 0);
    	cal.set(Calendar.MINUTE, 0);
    	cal.set(Calendar.SECOND, 0);
    	return cal.getTime();
    }
    /**
     * 获取本月最后一天23点59分59秒
    * @Title：getTheLastDayOfMonth 
    * @Description：TODO
    * @param ：@return 
    * @return ：Date 
    * @throws
     */
    public static Date getTheLastDayOfMonth(){
    	Calendar cal = Calendar.getInstance();
    	cal.add(Calendar.MONTH, 0);
    	cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    	cal.set(Calendar.HOUR_OF_DAY, 23);
    	cal.set(Calendar.MINUTE, 59);
    	cal.set(Calendar.SECOND, 59);
    	return cal.getTime();
    }
    /**
     * 获取上月最后一天23点59分59秒
     * @Title：getTheLastDayOfMonth 
     * @Description：TODO
     * @param ：@return 
     * @return ：Date 
     * @throws
     */
    public static Date getTheLastDayOfLastMonth(){
    	Calendar cal = Calendar.getInstance();
    	cal.add(Calendar.MONTH, -1);
    	cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    	cal.set(Calendar.HOUR_OF_DAY, 23);
    	cal.set(Calendar.MINUTE, 59);
    	cal.set(Calendar.SECOND, 59);
    	return cal.getTime();
    }


    /**
     * 根据生日获取年龄
     * @param birthday
     * @return 年龄
     */
    public static Integer getAgeByBirth(Date birthday) {
        int age;
        try {
            Calendar now = Calendar.getInstance();
            now.setTime(new Date());    // 当前时间
            Calendar birth = Calendar.getInstance();
            birth.setTime(birthday);

            if (birth.after(now)) {     //如果传入的时间，在当前时间的后面，返回0岁
                age = 0;
            } else {
                age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
                if (now.get(Calendar.DAY_OF_YEAR) > birth.get(Calendar.DAY_OF_YEAR)) {
                    age += 1;
                }
            }
            return age;
        } catch (Exception e) {         //兼容性更强,异常后返回数据
            return 0;
        }
    }

    /**
     * 获取几天后的时间
     * @param day
     * @return
     */
    public static Date getDateAfterDay(int day) {
        Date now = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.DATE, day);
        return cal.getTime();
    }

    /**
     * 获取几小时后的时间
     * @param hour
     * @return
     */
    public static Date getDateAfterHour(int hour) {
        Date now = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.HOUR, hour);
        return cal.getTime();
    }

    /**
     * 获取当前日期，多少天内的日期集合
     *
     * @param count
     * @return
     */
    public static List<String> dateList(int count){
        Calendar c = Calendar.getInstance();
        List<String> dateList = new LinkedList<>();
        for (int n = 0; n < count; n++) {
            c.setTime(new Date());
            c.add(Calendar.DATE, -n);
            Date d = c.getTime();
            String date = dateToString(d, DATE);
            dateList.add(date);
        }
        return dateList;
    }

    /**
     * 获取当前日期，多少周内的周集合
     *
     * @param count
     * @return
     */
    public static List<String> weekList(int count){
        List<String> weekList = new LinkedList<>();
        Calendar cl = Calendar.getInstance();
        cl.setTime(new Date());
        for (int n = 0; n < count; n++) {
            if (n > 0) {
                cl.add(Calendar.DATE, -7);
            } else {
                cl.add(Calendar.DATE, 0);
            }
            int week = cl.get(Calendar.WEEK_OF_YEAR);
            int year = cl.get(Calendar.YEAR);
            if (week < cl.get(Calendar.WEEK_OF_YEAR)) {
                year += 1;
            }
            String time = null;
            if (week < 10) {
                time = year + "-0" + week;
            } else {
                time = year + "-" + week;
            }
            weekList.add(time);
        }
        return weekList;
    }

    /**
     * 获取当前日期，多少月内的月集合
     *
     * @param count
     * @return
     */
    public static List<String> monthList(int count){
        String dateString;
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        List<String> rqList = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            dateString = sdf.format(cal.getTime());
            rqList.add(dateString.substring(0, 7));
            cal.add(Calendar.MONTH, -1);
        }
        System.out.println(rqList);
        return rqList;
    }


    public static Long calculateTime(Date endTime){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(endTime);
        long timeInMillis = calendar.getTimeInMillis();
        //当前系统时间
        long systime = System.currentTimeMillis();
        return (timeInMillis - systime);
    }


    /**
     *
     * @param count
     * @return
     */
    public static String getDate(Integer count){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -count);
        return dateToString(calendar.getTime(),"yyyy-MM-dd");
    }
    /**
     * 获取几分钟后的时间
     * @param minute
     * @return
     */
    public static String getDateAfterMinute(int minute) {
        Date now = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.MINUTE, minute);
        return dateToString(cal.getTime(),"yyyyMMddHHmmss");
    }

    /**
     * 获取指定年月的第一天
     * @param year
     * @param month
     * @return
     */
    public static String getFirstDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最小天数
        int firstDay = cal.getMinimum(Calendar.DATE);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH,firstDay);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(cal.getTime());
    }

    /**
     * 根据月份获得该月的开始时间和结束时间
     * @return
     * str 格式yyyy-mm
     */
    public static Map<String, String> getMothBeginAndEnd(String str) throws ParseException {
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM");
        Map<String, Date> condition = new HashMap<>();
        Map<String, String> condition1 = new HashMap<>();
        Date date =sdf.parse(str);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        condition.put("startTime",calendar.getTime());
        calendar.add(Calendar.DAY_OF_WEEK, 7);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 12);
        condition.put("stopTime",calendar.getTime());
        Date startTime = condition.get("startTime");
        Date stopTime = condition.get("stopTime");
        String s = DateUtils.dateToString(startTime, "yyyy-MM-dd");
        String s1 = DateUtils.dateToString(stopTime, "yyyy-MM-dd");
        condition1.put("startTime", s);
        condition1.put("stopTime", s1);

        return condition1;

    }
    public static void main(String[] args) throws ParseException {
        String str = "2020-03";
        String substring = str.substring(5, 7);

//        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM");
//        Map<String, Date> condition = new HashMap<>();
//        Date date =sdf.parse(str);
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(date);
//        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
//        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
//        condition.put("startTime",calendar.getTime());
//        calendar.add(Calendar.DAY_OF_WEEK, 7);
//        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
//        calendar.set(Calendar.HOUR_OF_DAY, 12);
//        condition.put("stopTime",calendar.getTime());
//        Date startTime = condition.get("startTime");
//        Date stopTime = condition.get("stopTime");
//        String s = DateUtils.dateToString(startTime, "yyyy-MM-dd");
//        String s1 = DateUtils.dateToString(stopTime, "yyyy-MM-dd");

        System.out.println(substring);
    }


    public static boolean checkSameMonth(String date1,String date2){
        String substring = date1.substring(5, 7);
        String substring1 = date2.substring(5, 7);
        if(substring.equals(substring1)){
            return true;
        }
        return false;
    }
}
